package Vista;

import Control.ControlModulo;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class frmMantenerModulo extends javax.swing.JFrame {

    private String viejoNombre;

    
    
    public String getViejoNombre() {
        return viejoNombre;
    }

    public void setViejoNombre(String viejoNombre) {
        this.viejoNombre = viejoNombre;
    }

    
    public frmMantenerModulo() {
        initComponents();
        //this.setIconImage(new ImageIcon(getClass().getResource("/Media/logo1.png")).getImage());
    }

    
    
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tbpaneMantenerModulo = new javax.swing.JTabbedPane();
        panelRegistrarModulo = new javax.swing.JPanel();
        txtModulo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        btnRegistrar = new javax.swing.JButton();
        panelVerModulos = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblModulos = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("..::  Sonrisas Tools :..    Módulos");

        tbpaneMantenerModulo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbpaneMantenerModuloMouseClicked(evt);
            }
        });
        tbpaneMantenerModulo.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                tbpaneMantenerModuloAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        panelRegistrarModulo.setBackground(new java.awt.Color(204, 204, 240));

        jLabel1.setText("Nombre módulo:");

        jLabel2.setText("Descripción:");

        txtDescripcion.setColumns(20);
        txtDescripcion.setRows(5);
        jScrollPane1.setViewportView(txtDescripcion);

        btnRegistrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Media/adduser1.png"))); // NOI18N
        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelRegistrarModuloLayout = new javax.swing.GroupLayout(panelRegistrarModulo);
        panelRegistrarModulo.setLayout(panelRegistrarModuloLayout);
        panelRegistrarModuloLayout.setHorizontalGroup(
            panelRegistrarModuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelRegistrarModuloLayout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(panelRegistrarModuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(panelRegistrarModuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRegistrar)
                    .addGroup(panelRegistrarModuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane1)
                        .addComponent(txtModulo)))
                .addContainerGap(247, Short.MAX_VALUE))
        );
        panelRegistrarModuloLayout.setVerticalGroup(
            panelRegistrarModuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelRegistrarModuloLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(panelRegistrarModuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtModulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(panelRegistrarModuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(btnRegistrar)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        tbpaneMantenerModulo.addTab("Registrar módulo", panelRegistrarModulo);

        panelVerModulos.setBackground(new java.awt.Color(204, 204, 240));

        jToolBar1.setBackground(new java.awt.Color(204, 204, 240));
        jToolBar1.setRollover(true);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Media/edit_reserva.png"))); // NOI18N
        jButton1.setText("Editar");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Media/cancel_reserva.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.setFocusable(false);
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnEliminar);

        tblModulos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Módulo", "Descripcion", "Fecha creacion"
            }
        ));
        jScrollPane2.setViewportView(tblModulos);

        javax.swing.GroupLayout panelVerModulosLayout = new javax.swing.GroupLayout(panelVerModulos);
        panelVerModulos.setLayout(panelVerModulosLayout);
        panelVerModulosLayout.setHorizontalGroup(
            panelVerModulosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelVerModulosLayout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(panelVerModulosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelVerModulosLayout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(panelVerModulosLayout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                        .addGap(73, 73, 73))))
        );
        panelVerModulosLayout.setVerticalGroup(
            panelVerModulosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelVerModulosLayout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(51, Short.MAX_VALUE))
        );

        tbpaneMantenerModulo.addTab("Ver módulos", panelVerModulos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tbpaneMantenerModulo)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tbpaneMantenerModulo, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
          
        if (btnRegistrar.getText().equals("Registrar")){
            ControlModulo controlModulo=new ControlModulo();
           boolean inserto = controlModulo.agregarModulo(txtModulo.getText(), txtDescripcion.getText());
           
                if (inserto){
                        JOptionPane.showMessageDialog(this, "Se registró el módulo correctamente", "Registro completo!", JOptionPane.INFORMATION_MESSAGE);
               
                            }else{
                        JOptionPane.showMessageDialog(this, "No se pudo registrar el módulo", "Error!", JOptionPane.ERROR_MESSAGE);
                                }
        }else{
           ControlModulo controlModulo=new ControlModulo();
           
            boolean actualizo = controlModulo.editarModulo(txtModulo.getText(), txtDescripcion.getText(),getViejoNombre());
            
                if (actualizo){
                    JOptionPane.showMessageDialog(this, "Se actualizó el módulo correctamente", "Registro completo!", JOptionPane.INFORMATION_MESSAGE);
                    //tbpaneMantenerModulo.mouse
                    tbpaneMantenerModuloMouseClicked(null);
                    tbpaneMantenerModulo.setSelectedIndex(1);
                
                }else {
                    JOptionPane.showMessageDialog(this, "No se pudo actualizar el módulo", "Error!", JOptionPane.ERROR_MESSAGE);
                }
            
        
        
        }
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        tbpaneMantenerModulo.setSelectedIndex(0);

        setViejoNombre("" + tblModulos.getValueAt(tblModulos.getSelectedRow(), 0));
        //Seteamos en las cajas de texto, los datos seleccionados en la tabla.
        txtModulo.setText("" + tblModulos.getValueAt(tblModulos.getSelectedRow(), 0));
        txtDescripcion.setText("" + tblModulos.getValueAt(tblModulos.getSelectedRow(), 1));
        

        btnRegistrar.setText("Modificar");



    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        int resp = JOptionPane.showConfirmDialog(this, "Desea eliminar el tratamiento seleccionado?", "Eliminar tratamiento", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (resp == 0) {
            int n = tblModulos.getSelectedRow();

            ControlModulo control = new ControlModulo();

            String nombre = "" + tblModulos.getValueAt(n, 0);
            if (control.eliminarModulo(nombre)) {
                JOptionPane.showMessageDialog(this, "Se eliminó el tratamiento correctamente", "Tratamiento eliminado", JOptionPane.INFORMATION_MESSAGE);
                limpiarTabla();
                tbpaneMantenerModuloAncestorAdded(null);
            }
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void tbpaneMantenerModuloAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_tbpaneMantenerModuloAncestorAdded
            limpiarTabla();
            ControlModulo control=new ControlModulo();
            ArrayList<ArrayList> listado = control.listarTodos();

            for (int i = 0; i < listado.size(); i++) {
                    tblModulos.setValueAt(listado.get(i).get(1), i, 0);
                    tblModulos.setValueAt(listado.get(i).get(2), i, 1);
                    tblModulos.setValueAt(listado.get(i).get(3), i, 2);
                }
    }//GEN-LAST:event_tbpaneMantenerModuloAncestorAdded

    private void tbpaneMantenerModuloMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbpaneMantenerModuloMouseClicked
        limpiarTabla();
            ControlModulo control=new ControlModulo();
            ArrayList<ArrayList> listado = control.listarTodos();

            for (int i = 0; i < listado.size(); i++) {
                    tblModulos.setValueAt(listado.get(i).get(1), i, 0);
                    tblModulos.setValueAt(listado.get(i).get(2), i, 1);
                    tblModulos.setValueAt(listado.get(i).get(3), i, 2);
                }
    }//GEN-LAST:event_tbpaneMantenerModuloMouseClicked

    public void limpiarTabla(){
        for (int i = 0; i < tblModulos.getRowCount(); i++) {
            tblModulos.setValueAt("", i, 0);
            tblModulos.setValueAt("", i, 1);
            tblModulos.setValueAt("", i, 2);
        }
    }
    
    
    
    
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmMantenerModulo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmMantenerModulo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmMantenerModulo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmMantenerModulo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmMantenerModulo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JPanel panelRegistrarModulo;
    private javax.swing.JPanel panelVerModulos;
    private javax.swing.JTable tblModulos;
    private javax.swing.JTabbedPane tbpaneMantenerModulo;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtModulo;
    // End of variables declaration//GEN-END:variables
}
